import {Component, OnInit} from '@angular/core';
import {OperateSericve} from '../operate-user.service';
import {Router, ActivatedRoute, Params} from '@angular/router';
import {LoadingService} from '../../../shared/loading.service';
import {AlertService} from '../../../shared/alert.service';
import {Md5} from 'ts-md5/dist/md5';

@Component({
    selector: 'app-operate-modify',
    templateUrl: './operate-modify.component.html',
    styleUrls: ['./operate-modify.component.scss']
})
export class OperateModifyComponent implements OnInit {
    public transmissionId: number;
    // 判断还修改还是添加
    public issuccess: Boolean = false;
    public isModalShown: Boolean = false;
    // 提示语
    public modelBody: string;
    // 保存用户角色信息
    public userRoles: Array<any> = [];
    // 绑定添加数据
    public AdministrationData = {
        number: '',
        name: '',
        email: '',
        role: 1,
    };
    public schoolnames: Array<any> = [];  // 学校名称
    public campushead: Boolean = false; // 校区负责人
    public selectedschool: any;  // 选中的学校

    public isedit: Boolean;  //  判断是否编辑

    constructor(private route: ActivatedRoute,
                private router: Router,
                private service: OperateSericve,
                public loading: LoadingService,
                public alert: AlertService) {
    }

    ngOnInit() {
        // this.loading.show();
        this.route.params.subscribe(params => {
            console.log(params.id);
            this.transmissionId = params.id;
        });
        if (this.transmissionId !== undefined) {
            const params: any = {
                token: localStorage.token,
                id: this.transmissionId,
            };
            this.service.readUsers(params).subscribe(
                res => {
                    console.log(res);
                    this.AdministrationData.number = res['data']['account'];
                    this.AdministrationData.name = res['data']['name'];
                    this.AdministrationData.role = res['data']['roles'];
                    this.selectedschool = res['data']['school'];
                    console.log(this.selectedschool);
                    this.isedit = false;
                    if (this.AdministrationData.role == 3) {
                        this.isedit = true;
                        this.campushead = true;
                    }
                },
                error => {
                    this.loading.hide();
                    this.alert.show('网络错误');
                }
            );
        }
    }

    returnHistory() {
        window.history.go(-1);
    }

    // 确定添加或者编辑
    determine() {
        console.log(this.AdministrationData);
        this.loading.show();
        if (this.AdministrationData.name === '') {
            this.isModalShown = true;
            this.modelBody = '姓名不能为空';
            this.issuccess = false;
        } else if (this.AdministrationData.number === '') {
            this.isModalShown = true;
            this.modelBody = '账号不能为空';
            this.issuccess = false;
        } else if (this.AdministrationData.email === '') {
            this.isModalShown = true;
            this.modelBody = '邮箱不能为空';
            this.issuccess = false;
        } else {
            if (this.transmissionId !== undefined) {
                const params: any = {
                    account: this.AdministrationData.number,
                    name: this.AdministrationData.name,
                    rid: this.AdministrationData.role,
                    id: this.transmissionId,
                    school: this.selectedschool,
                };
                console.log(params, '提交信息');
                this.service.updateUsers(params).subscribe(
                    res => {
                        console.log(res);
                        if (res['code'] === 0) {
                            this.isModalShown = true;
                            this.modelBody = '编辑成功';
                            this.issuccess = true;
                        } else {
                            this.isModalShown = true;
                            this.modelBody = res['msg'];
                            this.issuccess = false;
                        }
                        setTimeout(() => {
                            this.loading.hide();
                        }, 300);
                    },
                    error => {
                        this.loading.hide();
                        this.alert.show('网络错误');
                    }
                );
            } else {
                const params: any = {
                    username: this.AdministrationData.name,
                    rid: this.AdministrationData.role,
                    password: '123456',
                    email: this.AdministrationData.email
                };
                // console.log(params, '添加用户');
                this.service.saveUsers(params).subscribe(
                    res => {
                        console.log(res);
                        if (res['code'] === 0 || res['code'] === 3004) {
                            this.isModalShown = true;
                            this.modelBody = '添加成功';
                            this.issuccess = true;
                        } else {
                            this.isModalShown = true;
                            this.modelBody = res['msg'];
                            this.issuccess = false;
                        }
                        setTimeout(() => {
                            this.loading.hide();
                        }, 300);
                    },
                    error => {
                        this.loading.hide();
                        this.alert.show('网络错误');
                    }
                );
            }
        }
    }

    confirmshutdown() {
        this.loading.show();
        this.isModalShown = false;
        if (this.issuccess === true) {
            window.history.go(-1);
        }
        this.loading.hide();
    }


}
